/*
 * 
 *
 *
 */
package com.hboxs.asl.dao.impl;

import com.hboxs.asl.dao.RecommendCommodityDao;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.entity.RecommendCommodity;
import org.springframework.stereotype.Repository;

import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import java.util.List;

/**
 * Dao - 反馈信息
 */
@Repository("recommendCommodityDaoImpl")
public class RecommendCommodityDaoImpl extends BaseDaoImpl<RecommendCommodity, Long> implements RecommendCommodityDao {

    @Override
    public List<RecommendCommodity> findCurRecomCmdt() {

        String queryStr = "SELECT recommendCommodity " +
                "FROM RecommendCommodity As recommendCommodity " +
                "WHERE recommendCommodity.enable=true AND (current_timestamp BETWEEN recommendCommodity.startDate AND recommendCommodity.endDate) " +
                "ORDER BY recommendCommodity.createDate ";
        List<RecommendCommodity> recommendCommodities = entityManager.createQuery(queryStr).getResultList();

        return recommendCommodities;
    }

    @Override
    public RecommendCommodity findByProduct(Product product) {
        if (product == null) {
            return null;
        }
        String jpql = "select recommendCommodity from RecommendCommodity recommendCommodity where lower(recommendCommodity.product) = lower(:product)";
        try {
            return entityManager.createQuery(jpql, RecommendCommodity.class).setFlushMode(FlushModeType.COMMIT).setParameter("product", product).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }
}
